package com.gome.axwl.aoc.web.controller.driver;

import com.anxl.sso.client.api.AccountInfo;
import com.anxl.sso.client.api.Token;
import com.anxl.sso.client.support.AnxlSsoSupport;
import com.gome.axwl.aoc.common.constants.LoginConstant;
import com.gome.axwl.aoc.entity.dto.*;
import com.gome.axwl.aoc.service.GradeRuleService;
import com.gome.axwl.aoc.service.IntegrationRuleService;
import com.gome.axwl.aoc.service.sso.ServerGomeSsoClientService;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Description: 积分规则controller
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/4/25 15:55
 */
@Api(tags = "司机等级规则")
@RestController
@RequestMapping(value = "/driverGradeRule")
public class DriverGradeRuleController {

    @Autowired
    private IntegrationRuleService integrationRuleService;

    @Autowired
    private GradeRuleService gradeRuleService;

    @Autowired
    private ServerGomeSsoClientService ssoClientService;

    /**
     * 根据规则类别和日期查询规则设置信息
     * @param date 时间 yyyy-MM 的字符串
     * @param ruleType 积分规则类别 0：好评率  1：准时送达率 2：接单天数
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机等级规则-查询积分规则",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间", paramType = "query",dataType="String",required = true),
            @ApiImplicitParam(name = "ruleType", value = "积分规则类别 0：好评率  1：准时送达率 2：接单天数", paramType = "query",dataType="Byte",required = true)
    })
    @RequestMapping(value = "/queryIntegraRulesByType",method = RequestMethod.POST)
   public ResultData<QueryResultAndModifyStatusDto<IntegrationRuleDto>> queryIntegrationRulesByType(@RequestBody IntegrationRuleDto integrationRuleDto){
        return integrationRuleService.queryIntegrationRulesByType(integrationRuleDto.getRuleDate(),integrationRuleDto.getRuleType());
    }

    /**
     * 修改积分规则
     * @param integrationParamDto
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机等级规则-修改积分规则",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间", paramType = "query",dataType="String",required = true),
            @ApiImplicitParam(name = "ruleType", value = "积分规则类别 0：好评率  1：准时送达率 2：接单天数", paramType = "query",dataType="Byte",required = true)
    })
    @RequestMapping(value = "/modifyIntegraRulesByType",method = RequestMethod.POST)
    public ResultData modifyIntegraRulesByType(@RequestBody IntegrationParamDto integrationParamDto, HttpServletRequest request, HttpServletResponse response){
        AccountInfo accountInfo = (AccountInfo)request.getSession().getAttribute(LoginConstant.LOGIN_USER);
        if(accountInfo == null) {
            Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
            accountInfo = ssoClientService.getUserByLocalUserId(token.getUid(), request, response);
        }
        integrationParamDto.setModifyUserCode(accountInfo.getAccountNo());
        return integrationRuleService.modifyIntegrationRules(integrationParamDto);
    }

    /**
     * 根据日期查询等级设置信息
     * @param date 时间 yyyy-MM 的字符串
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机等级规则-查询等级规则",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间", paramType = "query",dataType="String",required = true)
    })
    @RequestMapping(value = "/queryGradeRule",method = RequestMethod.POST)
    public ResultData<QueryResultAndModifyStatusDto<GradeRuleDto>> queryGradeRule(@RequestBody GradeRuleDto gradeRuleDto){
        return gradeRuleService.queryGradeRules(gradeRuleDto.getGradeDate());
    }

    /**
     * 修改等级规则
     * @param gradeRuleParamDto
     * @author yinyuxin
     * @return
     */
    @ApiOperation(value = "司机等级规则-修改等级规则",  notes = "development by yinyuxin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间", paramType = "query",dataType="String",required = true)
    })
    @RequestMapping(value = "/modifyGradeRule",method = RequestMethod.POST)
    public ResultData modifyGradeRule(@RequestBody GradeRuleParamDto gradeRuleParamDto,HttpServletRequest request, HttpServletResponse response){
        AccountInfo accountInfo = (AccountInfo)request.getSession().getAttribute(LoginConstant.LOGIN_USER);
        if(accountInfo == null) {
            Token token = (Token) request.getSession().getAttribute(AnxlSsoSupport.SSO_CONTEXT);
            accountInfo = ssoClientService.getUserByLocalUserId(token.getUid(), request, response);
        }
        gradeRuleParamDto.setModifyUserCode(accountInfo.getAccountNo());
        return gradeRuleService.modifyGradeRules(gradeRuleParamDto);
    }


}
